package carltechnology.com.mahjonggame.utils.views;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import carltechnology.com.mahjonggame.R;

/**
 * Created by WZTCM on 2016/6/17.
 */
public class RoundTextView extends View {
    enum ROUND_CONSTANT{
        //控件的宽度高度最小值
        DEFAULT_TEXT_SIZE(20),
        DEFAULT_VIEW_SIZE(400),
        DEFAULT_BK_COLOR(Color.YELLOW),
        DEFAULT_TEXT_COLOR(Color.GRAY),
        DEFAULT_MIN_SIZE(50);
        private int constant;
        ROUND_CONSTANT(int constant){
            this.constant = constant;
        }
        public int getConstant(){
            return constant;
        }
    }
    //
    //private String mTextContent;

    private String mTextContent;

    private int mViewSize;
    private int mBkColor ;
    private int mViewLRPadding;
    private int mViewTBPadding;



    private int mTextSize;
    private int mTextColor;
//    private int mTextLRPadding;
//    private int mTextTBPadding;

    //控件用的paint
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private TextPaint textPaint = new TextPaint(paint);


    private Context mContext;
    public RoundTextView(Context context) {
        super(context,null);
        getDefalutSet();
    }

    public RoundTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        getDefaultSet(attrs);


    }
    private void getDefalutSet(){
        mTextContent = "";
        mBkColor = ROUND_CONSTANT.DEFAULT_BK_COLOR.getConstant();
        mTextSize = ROUND_CONSTANT.DEFAULT_TEXT_SIZE.getConstant();
        mTextColor = ROUND_CONSTANT.DEFAULT_TEXT_COLOR.getConstant();
//       mTextLRPadding = 0;
//        mTextTBPadding = 0;

        mViewLRPadding = 0;
        mViewTBPadding = 0;

        mViewSize = ROUND_CONSTANT.DEFAULT_VIEW_SIZE.getConstant();
    }
    private void getDefaultSet(AttributeSet attrs){
        getDefalutSet();
        TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.RoundTextView);
        int n = a.getIndexCount();
        for (int i=0;i<n;i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.RoundTextView_roundTextContent:
                    this.mTextContent = a.getString(attr);
                    break;
                case R.styleable.RoundTextView_bkColor:
                    this.mBkColor = a.getColor(attr, Color.RED);
                    break;
                case R.styleable.RoundTextView_textSize:
                    // 默认字体10dp
                    this.mTextSize = a.getDimensionPixelSize(attr,
                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10,getResources().getDisplayMetrics()));
                    break;
                case R.styleable.RoundTextView_textColor:
                    this.mTextColor = a.getColor(attr,0x00000000);
                    break;
                case R.styleable.RoundTextView_viewLRPadding:
                    this.mViewLRPadding = a.getDimensionPixelSize(attr,0);
                    break;
                case R.styleable.RoundTextView_viewTBPadding:
                    this.mViewTBPadding = a.getDimensionPixelSize(attr,0);
                    break;
//                case R.styleable.RoundTextView_textLRPadding:
//                    this.mTextLRPadding = a.getDimensionPixelSize(attr,0);
//                    break;
//                case R.styleable.RoundTextView_textTBPadding:
//                    this.mTextTBPadding = a.getDimensionPixelSize(attr,0);
//                    break;
                default:
                    break;
            }
        }
        a.recycle();
    }
    public RoundTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public RoundTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        Rect bounds = new Rect();
        paint.setTextSize(mTextSize);
        paint.getTextBounds(mTextContent,0,mTextContent.length(),bounds);
        int textWidth = (int)(bounds.width() +0.5);
        int textHeight = (int) (bounds.height() + 0.5);

        int size = Math.max(textHeight,textWidth)+ Math.max(mViewLRPadding,mViewTBPadding)*2 +10 *2;
        if (size>mViewSize) mViewSize = size;
        if (widthMode==MeasureSpec.AT_MOST&&heightMode==MeasureSpec.AT_MOST){
            setMeasuredDimension(mViewSize,mViewSize);
        }else if (widthMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(mViewSize,heightSize);
        }else if (heightMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(widthSize,mViewSize);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        final int paddingleft =  getPaddingLeft();
//        final int paddingRight = getPaddingRight();
//        final int paddingTop = getPaddingTop();
//        final int paddingBottom = getPaddingBottom();
        int width = getWidth() - mViewLRPadding*2;
        int height = getHeight() - mViewTBPadding*2;

        int radius = Math.min(width,height)/2;
        paint.setColor(mBkColor);
        canvas.drawCircle(mViewLRPadding+width/2,mViewTBPadding+height/2,radius,paint);

        // 计算字体宽度和长度
        Rect bounds = new Rect();
        paint.setColor(mTextColor);
        paint.setTextSize(mTextSize);
        paint.getTextBounds(mTextContent,0,mTextContent.length(),bounds);
        float textWidth = bounds.width();
        float textHeight = bounds.height();

        canvas.drawText
                (mTextContent,getWidth()/2-textWidth/2,getHeight()/2+textHeight/2,paint);
    }
}
